package com.waimai1.waimai.common;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.waimai1.waimai.entity.MyOrder;
import com.waimai1.waimai.service.FlService;
import com.waimai1.waimai.service.MyOrderService;
import com.waimai1.waimai.service.WepayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@Slf4j
@Component
public class ListenFlOrderStatus {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FlService flService;
    @Autowired
    private MyOrderService orderService;
    @Autowired
    private WepayService wepayService;
    /**
     * 监听延迟消息
     */
    @Scheduled(fixedDelay = 10000)
    public void listenHFLoop() {
        // 获取一个到点的消息
        Set<String> set = redisTemplate.opsForSet().members("FlOrderSearch");
        // 如果没有，就等等
        if (set.isEmpty()) {
            // 继续执行
            return;
        }
        // 获取具体消息的key
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String tradeNo = iterator.next();
            Map<String, Object> bizMap=new HashMap<>();
            bizMap.put("customer_order_no",tradeNo);
//            查询订单
            Map<String,Object> res=(Map<String,Object>)flService.getData(bizMap,"fulu.order.info.get");
            log.info("-----查单结果------"+res.toString());
            LambdaQueryWrapper<MyOrder> orderLambdaQueryWrapper=new LambdaQueryWrapper<>();
            orderLambdaQueryWrapper.eq(MyOrder::getOutTradeNo,tradeNo);
            MyOrder order=orderService.getOne(orderLambdaQueryWrapper,false);
            Map<String,Object> result=(Map<String,Object>) JSON.parse(res.get("result").toString());
            if((Integer)res.get("code")==0){
                if("success".equals(result.get("order_state"))){
                    order.setStatus(Utils.STATUS_OVER);
                    order.setHwStatus("充值成功");
                }
                else if("failed".equals(result.get("order_state"))){
                    order.setStatus(Utils.STATUS_WAIT_REFUND);
                    order.setHwStatus("充值失败");
                    wepayService.allRefund(order);
                }else{
                    log.error(result.get("order_state").toString());
                    continue;
                }
            }else if((Integer)res.get("code")==4011){
                order.setStatus(Utils.STATUS_WAIT_REFUND);
                order.setHwStatus("订单异常");
            }else{
                log.error("查单错误码"+res.get("code"));
                continue;
            }
//            更新order状态
            orderService.updateById(order);
            //在set中删除
            redisTemplate.opsForSet().remove("FlOrderSearch", tradeNo);
        }
    }
}
